<?php

namespace App\Common\Libs\Dispatch;

use App\Common\Libs\Json;
use App\Console\BaseCommand;
use App\Consts\GlobalConsoleConst;

/**
 * Class Console
 * @package App\Common\Libs\Dispatch
 */
class Console
{

    /** @var $instance */
    public static $instance;

    /**
     * @var BaseCommand $client
     */
    public $client;

    /**
     * Queue constructor.
     * @param $class
     */
    public function __construct($class)
    {
        if (new $class instanceof BaseCommand) {
            $this->client = new $class;
        }
    }

    /**
     * Get instance
     *
     * @param string $class
     * @return Console
     */
    public static function getInstance(string $class)
    {
        if (isset(self::$instance[$class])) {
            return self::$instance[$class];
        }
        return self::$instance[$class]  = new self($class);
    }

    /**
     * Push data to command
     *
     * @param $data
     * @param $scene
     * @param string $driver
     * @return \Illuminate\Foundation\Bus\PendingDispatch|int
     */
    public function push($data, $scene, $driver = GlobalConsoleConst::DRIVER_TYPE_QUEUE)
    {
        $body = ['action' => $scene, 'json' => Json::encode($data)];
        return $this->client::dispatch($body, $driver);
    }

}
